<?php
class CommAction extends Action {
    public function _initialize(){
        // 判断是否登陆
        self::check_admin();
        // 判断权限
        self::check_priv();
        // 记录日志
        self::manage_log();
        // ip屏蔽
        self::check_ip();
        $this->assign('userid',session('userid'));
        $this->assign('roleid',session('roleid'));
        $this->assign('username',session('username'));

    }

    public  function check_priv()
    {
        $access = new Model('access');
        $node = new Model('node');
       /*
        $pname = $_GET["_URL_"][0];
        $cname = $_GET["_URL_"][1];

        echo "<br>--:".__ROOT__  ;
        echo "<br>--:".__APP__   ;
        echo "<br>--:".__GROUP__ ;
        echo "<br>--:".__URL__   ;
        echo "<br>--__ACTION__:".__ACTION__;
        echo "<br>--:".__SELF__  ;
        echo "<br>--:".ACTION_NAME ;
        echo "<br>--:".APP_PATH ;
        echo "<br>--:".APP_NAME;
        echo "<br>--GROUP_NAME:".GROUP_NAME.'<br>';
        echo "<br>--MODULE_NAME:".MODULE_NAME.'<br>';
        echo "<br>--ACTION_NAME:".ACTION_NAME.'<br>';
*/
        $g_name = GROUP_NAME;

        $m_name = MODULE_NAME;
        $cname = ACTION_NAME;


        $id=$node->where(array('g'=>$g_name,'level'=>array('eq',0)))->getField('id');

        //$id=$node->where(array('g'=>$pname,'level'=>array('neq',0)))->getField('id');
        $carr=array();
        if(session('roleid')== 1){
            $cid=$node->field('id')->where(array('g'=>$g_name))->limit(0,1000)->select();
            foreach($cid as $cval){
                $carr[]=$cval['id'];
            }
        }else{
            //$cid = $access->field('node_id')->where(array('pid'=>$id))->limit(0,1000)->select();
            $cid = $access->field('node_id')->where(array('roleid'=>session('roleid')))->limit(0,1000)->select();
            foreach($cid as $cval){
                $carr[]=$cval['node_id'];
            }
        }
        //var_dump($carr);
        if(!empty($cid)){
            $nodelist=$node->field('id,name,title,g,m,a,data')->where(array('id'=>array('in',$carr)))->select();
            $i=0;
            $egarr=array();
            foreach($nodelist as $nodeval){
                if($nodeval['name']==$cname){
                    $i=$i+1;
                }
                $egarr[$nodeval['id']]=$nodeval;
            }

            //var_dump($egarr);
            $this->assign('egarr',$egarr);
        }
    }

    /**
     * 判断用户是否已经登陆
     */
    final public function check_admin() {
        if(GROUP_NAME =='admin' && MODULE_NAME =='index' && in_array(ACTION_NAME, array('login', 'public_card'))) {
            return true;
        } else {
            $userid = session('userid');
            $roleid = session('roleid');
            if(!isset($userid) || !isset($roleid) || !$userid || !$roleid){
                //showmessage(L('admin_login'),'?m=admin&c=index&a=login');
                $this->error('您没有登陆，请重新登陆.',"__APP_PATH__index.php?g=Admin&m=Login&a=index");

            }
        }
    }

    /**
     * lujinfa
     * 2013-09-04
     * 系统记录日志
     */
    public function manage_log()
    {
        /**
         * 后期判断是否记录日志
         */
        $M=M('log');
        $M->update_user = session('userid');
        $M->update_time = get_time();
        $M->operate 	= 'login';
        $M->ip 			= get_client_ip();
        $M->status 		= '0';
        $M->add();
    }

    /**
     * lujinfa
     * 2013-09-04
     * ip屏蔽
     */
    public  function check_ip()
    {

    }


    public function _empty($action){
       $this->error('Unknow handle.',U('index'));
    }


    /**
     * 按父ID查找菜单子项
     * @param integer $parentid   父菜单ID
     * @param integer $with_self  是否包括他自己
     */
    final public static function admin_menu($parentid, $with_self = 0) {
        $parentid = intval($parentid);
        $menudb = new Model('node');
        $condition['pid']= $parentid;
        $condition['status']= 1;

        $result =$menudb->field('*')->where($condition)->limit('0,1000')->ORDER('sort ASC')->select();
        if($with_self) {
            $result2[] = $menudb->field('*')->where(array('id'=>$parentid))->find();;
            $result = array_merge($result2,$result);
        }
        //权限检查
        if(session('roleid') == 1) return $result;
        $array = array();
        //$privdb = pc_base::load_model('admin_role_priv_model');
        //$siteid = param::get_cookie('siteid');
        $privdb = M('access');
        foreach($result as $v) {
            $action = $v['a'];
            if(preg_match('/^public_/',$action)) {
                $array[] = $v;
            } else {
                if(preg_match('/^ajax_([a-z]+)_/',$action,$_match)) $action = $_match[1];
                //$r = $privdb->get_one(array('m'=>$v['m'],'c'=>$v['c'],'a'=>$action,'roleid'=>$_SESSION['roleid'],'siteid'=>$siteid));

                $r = $privdb->where(array('role_id'=>session('roleid'),'node_id'=>$v['node_id']));
                if($r) $array[] = $v;
            }
        }
        return $array;
    }

    /**
     * 获取菜单 头部菜单导航
     *
     * @param $parentid 菜单id
     */
    final public static function submenu($parentid = '', $big_menu = false,$type=0) {

        /*
        echo "<br>--GROUP_NAME:".GROUP_NAME.'<br>';
        echo "<br>--MODULE_NAME:".MODULE_NAME.'<br>';
        echo "<br>--ACTION_NAME:".ACTION_NAME.'<br>';
        */


        if(empty($parentid)) {
            $menudb = M('node');
            $r = $menudb->where(array('g'=>GROUP_NAME,'m'=>MODULE_NAME,'a'=>ACTION_NAME))->find();
            $parentid = $_GET['menuid'] = $r['id'];
        }
        $array = self::admin_menu($parentid,1);
        $numbers = count($array);
        if($numbers==1 && !$big_menu) return '';
        $string = '';

        foreach($array as $_value) {
             if($type==-1)
             {
                 if($_value['type']!=0)
                 {
                     continue;
                 }
             }
            if (!isset($_GET['s'])) {
                $classname = GROUP_NAME == $_value['g'] && MODULE_NAME == $_value['m'] && ACTION_NAME == $_value['a'] ? 'class="on"' : '';
            } else {
                $_s = !empty($_value['data']) ? str_replace('=', '', strstr($_value['data'], '=')) : '';
                $classname = GROUP_NAME == $_value['g'] && MODULE_NAME == $_value['m'] && ACTION_NAME == $_value['a'] && $_GET['s'] == $_s ? 'class="on"' : '';
            }
            if($_value['pid'] == 0 || $_value['g']=='') continue;
            if($classname) {
                $string .= "<a href='javascript:;' $classname><em>".$_value['title']."</em></a><span>|</span>";
            } else {
                $string .= "<a href='?g=".$_value['g']."&m=".$_value['m']."&a=".$_value['a']."&menuid=$parentid".'&'.$_value['data']."' $classname><em>".$_value['title']."</em></a><span>|</span>";
            }
        }
        $string = substr($string,0,-14);
        return $string;
    }



    public function upload(){
        import('@.ORG.Net.UploadFile');
        $upload             = new UploadFile();
        $upload->maxSize    = C('MAX_UPLOAD_FILE_SIZE');
        $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','xls','doc');
        $upload->savePath   = __PUBLIC__.'/Upload/';
        if(!$upload->upload()) {
            $this->error($upload->getErrorMsg());
        }else{
            $info = $upload->getUploadFileInfo();
            $this->import($info);
        }
    }

}